package cn.lysy.xiaoruan.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import cn.lysy.xiaoruan.R;
import cn.lysy.xiaoruan.base.BaseFragment;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment {

    private RadioGroup radioGroup;
    private HomeFragment homeFragment;
    private ClassFragment classFragment;
    private AddFragment addFragment;
    private BuyFragment buyFragment;
    private MineFragment mineFragment;

    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;

    public MainFragment() {
        // Required empty public constructor
    }

    private void replace(Fragment fragment){

        fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frameLayout,fragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        radioGroup=view.findViewById(R.id.radioGroup);

        fragmentManager=getChildFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.radio_home:
                        homeFragment=new HomeFragment();
                        replace(homeFragment);
                        break;
                    case R.id.radio_class:
                        classFragment=new ClassFragment();
                        replace(classFragment);
                        break;
                    case R.id.radio_add:
                        addFragment=new AddFragment();
                        replace(addFragment);
                        break;
                    case R.id.radio_buy:
                        buyFragment=new BuyFragment();
                        replace(buyFragment);
                        break;
                    case R.id.radio_mine:
                        mineFragment=new MineFragment();
                        replace(mineFragment);
                        break;
                }
            }
        });

        radioGroup.check(R.id.radio_home);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

}
